import Button from "components/Base/Button";
import Icon from "components/Base/Icon";
import DataTable from "components/Base/DataTable";
import Image from "components/Base/Image";
import {Switch} from "antd";

export default (self) => [

  {
    title: 'id',
    name: 'id',
    tableItem: {
    },
    formItem: {
      type: 'hidden',
    }
  },
  {
    title: '名称',
    name: 'name',
    tableItem: {
    },
    formItem: {
      rules: [
        {
          required: true,
          message: '请输入商品名称'
        }
      ]
    },
  },
  {
    title: '图片',
    name: 'pic',
    tableItem: {
      render: (text, record) => {
        return <Image src={text} style={{width:50, height:50}}/>
      }
    },
    formItem: {
      type: 'upload',
      listType: 'picture',
      max: 1,
      rules: [
        {
          required: true,
          message: '请上传商品图片'
        }
      ]
    }
  },

  {
    title: '轮播图',
    name: 'carouselPics',
    // tableItem: {
    //   render: (text, record) => {
    //     return <Image src={text} style={{width:50, height:50}}/>
    //   }
    // },
    formItem: {
      type: 'upload',
      listType: 'picture',
      max: 5,
      rules: [
        {
          required: true,
          message: '请上传商品轮播图'
        }
      ]
    }
  },

  {
    title: "计量单位",
    name: 'unit',
    formItem: {
      rules: [
        {
          required: true,
          message: '请输入计量单位'
        }
      ]
    },
  },

  {
    title: "销量",
    name: 'saleCount',
    tableItem: {
    },
  },

  {
    title: '商品类目',
    name: 'goodTypeId',
    tableItem: {},
    formItem: {
      rules: [
        {
          required: true,
          message: '请选择商品类目'
        }
      ],

      showSearch: true,
      type: 'select',
      loading: self.getAll("goodType").loading,  // 加载状态
      onDropdownVisibleChange : (visible) =>{
        if (visible){
          self.dispatch({
            type: `goodType/getAll`,
          })
        }
      },
    },
    dict: self.getAll("goodType").dict,
  },

  {
    title: "品牌名称",
    name: 'brand',
    formItem: {
    },
  },

  {
    title: "分享标题",
    name: 'shareTitle',
    formItem: {
    },
  },

  {
    title: '是否热门',
    name: 'isHot',
    dict: [
      { code: false, codeName: '不热门' },
      { code: true, codeName: '上热门' },
    ],
    tableItem: {
    },
    formItem: {
      type: 'select',
      rules: [{ required: true, message: '请选择是否上热门' }]
    }
  },

  {
    title: '推荐指数',
    name: 'star',
    tableItem: {
    },
    formItem: {
      type: 'number',
      max: 5,
      rules: [{ required: true, message: '请输入推荐指数' }]
    }
  },

  {
    title: '状态',
    name: 'soldOut',
    tableItem: {
      render: (text, record) => {
        const {soldOut} = record;
        return  <Switch checkedChildren="上架" unCheckedChildren="下架"
                        defaultChecked={!soldOut} onChange={(checked) =>self.props.soldOut(record.id, !checked)}/>
      }
    },
  },

  {
    title: '商品描述',
    name: 'description',
    tableItem: {
      render: (text, record) => {
        return (
          <DataTable.Oper>
            <a onClick={e => {
              self.props.showGoodEditor(record.id, record.description);
            }}>
              <span>编辑描述</span>
            </a>
          </DataTable.Oper>
        );
      }
    },
  },

  {
    title: '商品规格',
    name: 'goodsItem',
    tableItem: {
      render: (text, record) => {

        return (
          <DataTable.Oper>
            <a onClick={e => {
              self.props.showGoodItem(record.id);
            }}>
              <span>编辑规格</span>
            </a>
          </DataTable.Oper>
        );
      }
    },
  },

  {
    title: '商品奖励',
    name: 'reward',
    tableItem: {
      render: (text, record) => {

        return (
          <DataTable.Oper>
            <a onClick={e => {
              self.props.showGoodReward(record.id);
            }}>
              <span>编辑奖励</span>
            </a>
          </DataTable.Oper>
        );
      }
    },
  },

  {
    title: '操作',
    tableItem: {
      width: 180,
      align: 'center',
      render: (text, record) => (
        <DataTable.Oper>
          <Button tooltip="修改" onClick={e => self.onUpdate(record)}>
            <Icon type="edit"/>
          </Button>
          <Button tooltip="删除" onClick={e => self.onDelete(record)}>
            <Icon type="trash"/>
          </Button>
        </DataTable.Oper>
      )
    }
  }
]
